import {axiosClient} from "@/utils/axiosClient.js";
import {handleCommonParams} from "@/api/commonParams.js";

const requestUrl = '/system/userRole'

const api = {

    /**
     * 新增用户-角色关联
     * @param userRole
     * @returns {Promise<*|undefined>}
     */
    createUserRole: (userRole) =>
      axiosClient().post(`${requestUrl}/addInfo`, userRole),

    /**
     * 根据ID删除用户-角色关联
     * @param id
     * @returns {Promise<*|undefined>}
     */
    deleteUserRoleById: (id) =>
        axiosClient().remove(`${requestUrl}/removeInfo/${id}`),

    /**
     * 根据ID全量更新用户-角色关联
     * @param id
     * @param userRole
     * @returns {Promise<*|undefined>}
     */
    updateUserRoleFull: (id, userRole) =>
        axiosClient().put(`${requestUrl}/modifyInfo/${id}`, userRole),

    /**
     * 根据ID部分更新用户-角色关联
     * @param id
     * @param updatePartial
     * @returns {Promise<*|undefined>}
     */
    updateUserRolePartial: (id, updatePartial) =>
        axiosClient().patch(`${requestUrl}/modifyPartialInfo/${id}`, updatePartial),

    /**
     * 根据ID查询用户-角色关联
     * @param id
     * @returns {Promise<*|undefined>}
     */
    getUserRoleById: (id) =>
        axiosClient().get(`${requestUrl}/getInfo/${id}`),

    /**
     * 分页查询用户-角色关联
     * @param page
     * @param size
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getUserRolePage: (page, size, businessParam) => {
        const params = new URLSearchParams();
        params.append('page', page)
        params.append('size', size)
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfos?${params.toString()}`)
    },

    /**
     * 根据条件查询用户-角色关联
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getUserRoleList: (businessParam) => {
        const params = new URLSearchParams();
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfoList?${params.toString()}`)
    }

}

const setBusinessParams = (params, businessParam) => {
    if (businessParam) {
        if (businessParam.userIdentityId) {
            params.append('userIdentityId', businessParam.userIdentityId)
        }
        if (businessParam.roleId) {
            params.append('roleId', businessParam.roleId)
        }
        if (businessParam.expireTime) {
            params.append('expireTime', businessParam.expireTime)
        }
        handleCommonParams(params, businessParam);
    }
}

export default api
